package com.random;

/**
 * 随机生成数字 测试概率
 * 思想：初始化每一个级别的权重（概率），每次生成一个随机数，判断该随机数在某一个概率区间，则表示中奖该区间
 * @author yaojinshan
 *
 */
public class TestRandom {
	static int max_num = 10000;//总权重
	static double LEVEL_1 = Double.parseDouble(String.valueOf(1.0d / max_num));//第一级别概率
	static double LEVEL_2 = Double.parseDouble(String.valueOf(11.0d / max_num));//第二级别概率
	static double LEVEL_3 = Double.parseDouble(String.valueOf(111.0d / max_num));//第三级别概率
	static double LEVEL_4 = Double.parseDouble(String.valueOf(222.0d / max_num));//第四级别概率
	static double LEVEL_5 = Double.parseDouble(String.valueOf(333.0d / max_num));//第五级别概率
	static double LEVEL_6 = Double.parseDouble(String.valueOf(9322.0d / max_num));//第六级别概率
	static double times_1 = 0;//第一级别累计出现次数
	static double times_2 = 0;//第二级别累计出现次数
	static double times_3 = 0;//第三级别累计出现次数
	static double times_4 = 0;//第四级别累计出现次数
	static double times_5 = 0;//第五级别累计出现次数
	static double times_6 = 0;//第六级别累计出现次数
	static double total_times = 1000000;
	public static void main(String[] args) {
		double random = 0.0;
		for(int i = 0 ; i < total_times ; i++){
			random = Math.random();
			if(0.0d <= random && random < LEVEL_1){//0.01%
				times_1++;
			}else if(LEVEL_1 <= random && random < (LEVEL_1 + LEVEL_2)){//0.01% - 0.12%
				times_2++;
			}else if((LEVEL_1 + LEVEL_2) <= random && random < (LEVEL_1 + LEVEL_2 + LEVEL_3)){// 0.12% - 1.23%
				times_3++;
			}else if((LEVEL_1 + LEVEL_2 + LEVEL_3) <= random && random < (LEVEL_1 + LEVEL_2 + LEVEL_3 + LEVEL_4)){//1.23% - 3.45%
				times_4++;
			}else if((LEVEL_1 + LEVEL_2 + LEVEL_3 + LEVEL_4) <= random && random < (LEVEL_1 + LEVEL_2 + LEVEL_3 + LEVEL_4 + LEVEL_5)){//3.45% - 6.78%
				times_5++;
			}else if((LEVEL_1 + LEVEL_2 + LEVEL_3 + LEVEL_4 + LEVEL_5) <= random && random < 1.00d){//6.78% - 100%
				times_6++;
			}
		}
		System.out.println("总计实验：" + total_times + "次");
		System.out.println("LEVEL_1: " + times_1 + "次出现,期望概率为："+ LEVEL_1 * 100 + "%" +",实验概率为：" + (times_1 / total_times) * 100 + "%");
		System.out.println("LEVEL_2: " + times_2 + "次出现,期望概率为："+ LEVEL_2 * 100 + "%" +",实验概率为：" + (times_2 / total_times) * 100 + "%");
		System.out.println("LEVEL_3: " + times_3 + "次出现,期望概率为："+ LEVEL_3 * 100 + "%" +",实验概率为：" + (times_3 / total_times) * 100 + "%");
		System.out.println("LEVEL_4: " + times_4 + "次出现,期望概率为："+ LEVEL_4 * 100 + "%" +",实验概率为：" + (times_4 / total_times) * 100 + "%");
		System.out.println("LEVEL_5: " + times_5 + "次出现,期望概率为："+ LEVEL_5 * 100 + "%" +",实验概率为：" + (times_5 / total_times) * 100 + "%");
		System.out.println("LEVEL_6: " + times_6 + "次出现,期望概率为："+ LEVEL_6 * 100 + "%" +",实验概率为：" + (times_6 / total_times) * 100 + "%");
	}

}
